package com.example.ass7.animation;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import biuoop.DrawSurface;
import biuoop.KeyboardSensor;

import com.example.ass7.Selection;

/**
 * @author Meirav Ben Izhak
 * @param <T>
 */
public class MenuAnimation<T> implements Menu<T> {
    private final String menuTitle = "Space Invaders";

    private final List<Selection<T>> selections = new ArrayList<Selection<T>>();
    private final List<Selection<Menu<T>>> menuSelections =
            new ArrayList<Selection<Menu<T>>>();
    private T status;
    private final KeyboardSensor sensor;
    private final AnimationRunner animationRunner;
    private boolean stop;

    /**
     * @param sensor
     *            sensor
     * @param animationRunner
     *            animationRunner
     */
    public MenuAnimation(
        KeyboardSensor sensor, AnimationRunner animationRunner) {

        this.sensor = sensor;
        this.animationRunner = animationRunner;
        this.stop = Boolean.FALSE;
    }

    /**
     * @param d d
     * @param color color
     * @param x x
     * @param y y
     * @param message message
     * @param size size
     */
    private void drawBorderText(DrawSurface d,
            Color color, int x, int y, String message, int size) {
        d.setColor(Color.black);
        d.drawText(x - 1,
                y,
                message,
                size);
        d.drawText(x + 1,
                y,
                message,
                size);
        d.drawText(x,
                y - 1,
                message,
                size);
        d.drawText(x,
                y + 1,
                message,
                size);
        d.setColor(color);
        d.drawText(x,
                y,
                message,
                size);
    }

    @Override
    public void doOneFrame(DrawSurface d,
            double dt) {
        d.setColor(new Color(192, 192, 192));
        d.fillRectangle(0,
                0,
                800,
                600);

        drawBorderText(d, new Color(255, 255, 0), 70, 60, this.menuTitle, 33);

        int xBegin = 130;
        int yBegin = 140;

        for (Selection<Menu<T>> menuSelection : this.menuSelections) {
            drawBorderText(d, Color.GREEN, xBegin, yBegin,
                    String.format("(%s) %s",
                    menuSelection.getKey(),
                    menuSelection.getMessage()), 23);
            yBegin = yBegin + 40;
            if (this.sensor.isPressed(menuSelection.getKey())) {
                Menu<T> menu = menuSelection.getReturnVal();
                this.animationRunner.run(menu);
                this.status = menu.getStatus();
                this.stop = Boolean.TRUE;
                return;
            }
        }

        for (Selection<T> selection : this.selections) {
//            d.drawText(xBegin,
//                    yBegin,
//                    String.format("(%s) %s",
//                            selection.getKey(),
//                            selection.getMessage()),
//                    27);
            drawBorderText(d, Color.GREEN,
                    xBegin, yBegin, String.format("(%s) %s",
                    selection.getKey(),
                    selection.getMessage()), 23);
            yBegin = yBegin + 40;
            if (this.sensor.isPressed(selection.getKey())) {
                this.status = selection.getReturnVal();
                this.stop = Boolean.TRUE;
                return;
            }
        }

    }

    @Override
    public boolean shouldStop() {
        if (this.stop) {
            this.stop = Boolean.FALSE;
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public T getStatus() {
        return this.status;
    }

    @Override
    public void addSelection(String key,
            String message,
            T returnVal) {
        this.selections.add(new Selection<T>(key, message, returnVal));
    }

    @Override
    public void addSubMenu(String key,
            String message,
            Menu<T> subMenu) {
        // this.selections
        // .add(new Selection<T>(key, message, subMenu.getStatus()));
        this.menuSelections.add(new Selection<Menu<T>>(key, message, subMenu));
    }
}
